package org.xbzheng.restful_study.dao;

import org.springframework.stereotype.Repository;
import org.xbzheng.restful_study.model.Resources;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import java.util.List;

@Repository
public class ResourceDao {

    @PersistenceContext
    private EntityManager entityManager;

    public List<Resources> findAll(){
        Query query = entityManager.createQuery("select r from Resources r");
        return query.getResultList();
    }

    public Resources save(Resources resources){
        return entityManager.merge(resources);
    }

    public void delete(Resources resources){
        entityManager.remove(resources);
    }

    public List<Resources> findTopRes(){
        Query query = entityManager.createQuery("select r from Resources r where r.parent is null ");
        return query.getResultList();
    }

    public List<Resources> findLeftMenu(String username, String resourceType){
        String sql = "(select re.id,re.resourceName,re.resourceVal,re.resourceInterceptStr,re.resourceType, re.cla, re.style, re.orderNum, re.parent_id, re.description from t_user u " +
                "left join t_user_role ur on u.id=ur.user_id " +
                "left join t_role r on ur.role_id = r.id " +
                "left join t_role_resources rr on r.id= rr.role_id " +
                "left join t_resources re on rr.resource_id = re.id where u.username = ?1 and re.resourceType=?2) " +
                "union " +
                "(select re.id,re.resourceName,re.resourceVal,re.resourceInterceptStr,re.resourceType, re.cla, re.style, re.orderNum, re.parent_id, re.description " +
                "from t_resources re where re.id in (select re.parent_id from t_user u left join t_user_role ur on u.id=ur.user_id " +
                "left join t_role r on ur.role_id = r.id left join t_role_resources rr on r.id= rr.role_id " +
                "left join t_resources re on rr.resource_id = re.id where u.username = ?3 and re.resourceType=?4 and re.parent_id is not null))";
        //Query query = entityManager.createQuery("select re from User u left join u.roles r left join r.resources re where u.username=?1 and re.resourceType=?2 order by re.orderNum");
        Query query = entityManager.createNativeQuery(sql, Resources.class);
        query.setParameter(1, username);
        query.setParameter(2, resourceType);
        query.setParameter(3, username);
        query.setParameter(4, resourceType);
        return query.getResultList();
    }

    public List<Resources> findPageResources(String username, String owner){
        String sql = "select * from t_user u left join t_user_role ur on u.id = ur.user_id " +
                    "left join t_role r on ur.role_id = r.id " +
                    "left join t_role_resources rr on r.role = rr.role_id " +
                    "left join t_resources re on rr.resource_id = re.id " +
                    "where u.username=?1 and re.resourceVal=?2";
        Query query = entityManager.createNativeQuery(sql, Resources.class);
        query.setParameter(1, username);
        query.setParameter(2, owner);

        return query.getResultList();
    }

}
